package com.example.pengsiyuan.ppbluetoothle.activity;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.Log;
import android.widget.TextView;

import com.example.pengsiyuan.ppbluetoothle.DBManager;
import com.example.pengsiyuan.ppbluetoothle.Interface.MainInterface;
import com.example.pengsiyuan.ppbluetoothle.R;
import com.example.pengsiyuan.ppbluetoothle.util.DataUtil;
import com.example.pengsiyuan.ppbluetoothle.util.PPUtil;
import com.peng.ppscalelibrary.BleManager.Interface.BleDataProtocoInterface;
import com.peng.ppscalelibrary.BleManager.Manager.BleManager;
import com.peng.ppscalelibrary.BleManager.Model.BleDeviceModel;
import com.peng.ppscalelibrary.BleManager.Model.BleEnum;
import com.peng.ppscalelibrary.BleManager.Model.BleUserModel;
import com.peng.ppscalelibrary.BleManager.Model.LFPeopleGeneral;

import java.util.ArrayList;
import java.util.List;

public class BindingDeviceActivity extends Activity {

    TextView weightTextView;
    BleManager bleManager;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_bingdevice);
        weightTextView = findViewById(R.id.weightTextView);
        bleManager = BleManager.shareInstance(getApplication());
        bindingDevice();
    }

    @Override
    protected void onPause() {
        super.onPause();
        bleManager.stopSearch();

    }

    private void bindingDevice(){

        List<BleDeviceModel> deviceList = DBManager.manager().getDeviceList();
        final BleUserModel userModel = DataUtil.util().getUserModel();
        bleManager.searchDevice(true, deviceList, userModel, new BleDataProtocoInterface() {
            @Override
            public void progressData(final LFPeopleGeneral bodyDataModel) {
                String weightStr = PPUtil.getWeight(userModel.unit, bodyDataModel.lfWeightKg);
                weightTextView.setText(weightStr);
            }

            @Override
            public void lockedData(LFPeopleGeneral bodyDataModel, BleDeviceModel deviceModel, boolean isHeartRating) {
                if (!isHeartRating) {
                    String weightStr = PPUtil.getWeight(userModel.unit, bodyDataModel.lfWeightKg);
                    weightTextView.setText(weightStr);
                    showDialog(deviceModel, userModel, bodyDataModel);
                }
            }

            @Override
            public void historyData(boolean isEnd, LFPeopleGeneral bodyDataModel, String date) {

            }

            @Override
            public void deviceInfo(BleDeviceModel deviceModel) {

            }
        });

    }

    private void showDialog(final BleDeviceModel deviceModel, BleUserModel userModel, final LFPeopleGeneral bodyDataModel) {
        String content = "是否保存当前体重" + PPUtil.getWeight(userModel.unit, bodyDataModel.lfWeightKg);
        AlertDialog.Builder builder = new AlertDialog.Builder(BindingDeviceActivity.this);
        builder.setTitle("这是您的数据么？");
        builder.setMessage(content);
        builder.setPositiveButton("确定", new DialogInterface.OnClickListener()
        {
            @Override
            public void onClick(DialogInterface dialog, int which)
            {
                DBManager.manager().insertDevice(deviceModel);
                DataUtil.util().setBodyDataModel(bodyDataModel);
                dismissSelf();
            }
        });
        builder.setNegativeButton("取消", new DialogInterface.OnClickListener()
        {
            @Override
            public void onClick(DialogInterface dialog, int which)
            {
                bleManager.reSearchDevice();
            }
        });
        builder.show();
    }

    private void dismissSelf(){
        bleManager.stopSearch();
        finish();
    }


}


